package com.seres.listener;

import com.seres.listener.event.ClearTableCacheEvent;
import com.seres.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @Description : 事务提交成功监听
 * @Author : 杨松
 * @Date: 2020-08-12 09:50
 */
@Slf4j
@Component
public class TransactionalCommitClearTableCacheListener  {

    @Autowired
    private RedisCache redisCache;

    @TransactionalEventListener(fallbackExecution = true,phase= TransactionPhase.AFTER_COMMIT)
    public void handleClearTableCacheEvent(ClearTableCacheEvent clearTableCacheEvent){
        try{
            long count = redisCache.getJedis().del(clearTableCacheEvent.getKey());
            log.info("成功删除:[{}]条缓存",count);
        }catch (Throwable e) {
            log.error("删除单表缓存出现异常:KEY:{},{}", clearTableCacheEvent.getKey(), e);
        }
    }
}
